package tester;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: aleck
 * Date: 2007-9-22
 * Time: 11:04:35
 * To change this template use File | Settings | File Templates.
 */
public class PersonManager {
  public final File programDir;
  public final ProblemManager problemManager;
  public final List<Person> personList = new ArrayList<Person>();

  public Person find(String personName) {
    for (Person person : personList) {
      if (person.name.equals(personName.toUpperCase())) return person;
    }
    return null;
  }

  public PersonManager(String programDirName, ProblemManager problemManager)
      throws InternalErrorException {
    this.problemManager = problemManager;
    programDir = new File(programDirName);
    if (!programDir.exists() || !programDir.isDirectory())
      return;
    File[] fileList = programDir.listFiles();
    for (File file : fileList) {
      if (file.isDirectory())
        personList.add(
            new Person(file.getAbsoluteFile(), problemManager));
    }
  }

  public int getPersonCount() {
    return personList.size();
  }

  public void saveToFiles() {
    try {
      for (Person person : personList) {
        person.saveToConfigFile();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
